/*
 * @Author: Haochen
 * @Date: 2021-11-21 17:50:50
 * @LastEditTime: 2021-11-26 11:01:21
 * @FilePath: \front\src\js\exam.js
 */

import $ from './jquery-3.6.0.min.js';
import '../scss/common.scss';
import '../scss/exam.scss';
import { backToIndex, getPromise } from './common.js';

backToIndex();

var testType = window.location.search.substring(1).split('=').pop();

switch (testType) {
  case 'formal':
    $(`<h3>正在考试题库</h3>`).insertAfter('.btn-back-to-index');
    break;
  case 'mock':
    $(`<h3>模拟考试题库</h3>`).insertAfter('.btn-back-to-index');
    break;
  case 'quiz':
    $(`<h3>每日一测题库</h3>`).insertAfter('.btn-back-to-index');
    break;
  case 'circular':
    $(`<h3>顺序练习题库</h3>`).insertAfter('.btn-back-to-index');
    break;
  case 'category':
    $(`<h3>分类考试题库</h3>`).insertAfter('.btn-back-to-index');
    break;

  default:
    break;
}

var strDataQuery = '/testsRouter/byType';

async function getDataAndRender() {
  // console.log('queryData: ', strDataQuery);

  const { code, data, message } = await getPromise(strDataQuery, 'get', {
    testType,
  });

  // console.log('dataBack: ', data);
  render(data);
}

getDataAndRender();

function render(tests) {
  $('.main-content').html('');
  tests.forEach((test) => {
    $('.main-content').append(`
        <div class="exam-box" data-id="${test._id}">
                <div class="eb-row">
                    ${test.title}
                </div>
                <div class="eb-row">
                    考试类型：<span>${test.typeId.type}</span>
                </div>
                <div class="eb-row">
                    考试地址：<span>信息中心会议室</span>
                </div>
                <div class="eb-row">
                    已考人数：<span>343人</span>
                </div>
                <div class="eb-row" data-id="${test._id}">
                    参加考试
                </div>
        </div>
  `);
  });
}
async function checkTestedAndRender() {
  const users = JSON.parse(localStorage.getItem('users'));

  const userId = users.find((user) => user.status === 'online')._id;

  let { code, data, message } = await getPromise(
    '/testedsRouter/:' + userId,
    'get',
    {}
  );
  // console.log(`dataBack from testedsRouter`, data);

  data = data.map((tested) => tested.testId._id);
  handle(data);
}
// const tested = getDataFrom('/testedsRouter/', {}, 'testedsRouter', handle);
checkTestedAndRender();

function handle(testedIds) {
  $('.exam-box').each(function () {
    testedIds.forEach((testedId) => {
      if ($(this).attr('data-id') === testedId) {
        const lastRow = $(this).children('.eb-row:last-of-type');
        lastRow.text('已考试，请看解析');
        lastRow.addClass('tested');
        lastRow.css('pointer-events', 'none');
      }
    });
  });
}

$('.btn-back-to-index').click(function () {
  location.href = './index.html';
});

$('.dropdown').mouseover(function () {
  $('.drop-list').show();
});

$('.dropdown').mouseout(function () {
  $('.drop-list').hide();
});

$('body').on('click', '.exam-box .eb-row:last-of-type', function () {
  location.href = `./exam_info.html?type=${testType}?id=${$(this).attr(
    'data-id'
  )}`;
});
